﻿/*
Copyright © 2011 Nokia Corporation. All rights reserved.
Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
Java and all Java-based marks are trademarks or registered trademarks of 
Sun Microsystems, Inc. Other product and company names mentioned herein may be 
trademarks or trade names of their respective owners.

Subject to the conditions below, you may, without charge:

·  Use, copy, modify and/or merge copies of this software and 
   associated content and documentation files (the “Software”)

·  Publish, distribute, sub-licence and/or sell new software 
   derived from or incorporating the Software.

Some of the documentation, content and/or software maybe licensed under open source software or other licenses.  To the extent such documentation, content and/or software are included, licenses and/or other terms and conditions shall apply in addition and/or instead of this notice. The exact terms of the licenses, disclaimers, acknowledgements and notices are reproduced in the materials provided.

This file, unmodified, shall be included with all copies or substantial portions
of the Software that are distributed in source code form.

The Software cannot constitute the primary value of any new software derived 
from or incorporating the Software.

Any person dealing with the Software shall not misrepresent the source of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.IO;
using System.Text;

namespace Carpool
{
    public class DC
    {
        private static DC dc = null;
        string url = null;

        internal delegate void PersonalProfileCallback(bool isok, string erromsg, DCPersonalProfile dcpersonalprofile);
        internal delegate void CarpoolListCallback(bool isok, string erromsg, DCCarPoolList dccarpoollist);
        internal delegate void UserListCallback(bool isok, string errormsg, DCUserList dcuserlist);
        internal delegate void MyTripListCallback(bool isok, string errormsg, DCMyTripList dcmytriplist);
        internal delegate void CarPoolActivationListCallback(bool isok, string errormsg, DCCarPoolActivationList dccarpoolactivation);
        internal delegate void CarPoolMembersListCallback(bool isok, string errormsg, DCCarPoolMembersList dccarpoolmemberslist);
        internal delegate void RouteCallback(bool isok, string errormsg, DCRoute dcroute);
        internal delegate void RouteListCallback(bool isok, string errormsg, DCRouteList dcroutelist);
        internal delegate void JoinRequestCallback(bool isok, string errormsg, DCJoinRequest dcjoinrequest);
        internal delegate void GetMessagesCallback(bool isok, string errormsg, DCMessageList dcmessagelist);
        internal delegate void KarpoIdListCallback(bool isok, string errormsg, DCKarpoIdList dckarpoidlist);
        internal delegate void SendMessageCallback(bool isok, string errormsg, DCMessageOut dcmessageout);

   
        public DC()
        {
            url = MainPage.datacloudurl + "/KarpoDC/";
        }

        internal static DC DataCloud()
        {
            if (dc == null)
            {
                dc = new DC();
            }
            return dc;
        }

        internal void Login(DCPersonalProfile dcclass, PersonalProfileCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(savepersonalprofile_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=login&json=" + DCPersonalProfileJsonUtil.WriteFromObject(dcclass), cb);

        }


        internal void SavePersonalProfile(DCPersonalProfile dcclass, PersonalProfileCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(savepersonalprofile_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=savepersonalprofile&json=" + DCPersonalProfileJsonUtil.WriteFromObject(dcclass), cb);

        }

        internal void DeletePersonalProfile(DCPersonalProfile dcclass, PersonalProfileCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(savepersonalprofile_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=deleteuseraccount&json=" + DCPersonalProfileJsonUtil.WriteFromObject(dcclass), cb);

        }

        void savepersonalprofile_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            PersonalProfileCallback cb = (PersonalProfileCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCPersonalProfile dcclass = DCPersonalProfileJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        internal void GetCarpoolsByOwner(DCCarPoolList dcclass, CarpoolListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(carpoolist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=getcarpoolsbyowner&json=" + DCCarPoolListJsonUtil.WriteFromObject(dcclass), cb);

        }

        internal void SaveCarpools(DCCarPoolList dcclass, CarpoolListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(carpoolist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=savecarpools&json=" + DCCarPoolListJsonUtil.WriteFromObject(dcclass), cb);
        }

        internal void DeleteCarpool(DCCarPoolList dcclass, CarpoolListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(carpoolist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=deletecarpool&json=" + DCCarPoolListJsonUtil.WriteFromObject(dcclass), cb);
        }

        void carpoolist_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            CarpoolListCallback cb = (CarpoolListCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCCarPoolList dcclass = DCCarPoolListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        internal void AddCarpoolMembers(DCCarPoolMembersList dcclass, CarPoolMembersListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(carpoomemberslist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=addcarpoolmembers&json=" + DCCarPoolMembersListJsonUtil.WriteFromObject(dcclass), cb);
        }


        void carpoomemberslist_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            CarPoolMembersListCallback cb = (CarPoolMembersListCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCCarPoolMembersList dcclass = DCCarPoolMembersListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        internal void GetDataCloudUsers(DCUserList dcclass, UserListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(userlist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=getdatacloudusers&json=" + DCUserListJsonUtil.WriteFromObject(dcclass), cb);
        }

        void userlist_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            UserListCallback cb = (UserListCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCUserList dcclass = DCUserListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        internal void GetMyTrips(DCMyTripList dcclass, MyTripListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(mytriplist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=getmytrips&json=" + DCMyTripListJsonUtil.WriteFromObject(dcclass), cb);
        }

        void mytriplist_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            MyTripListCallback cb = (MyTripListCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCMyTripList dcclass = DCMyTripListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        internal void GetCarPoolActivation(DCCarPoolActivationList dcclass, CarPoolActivationListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(carpoolactivation_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=getcarpoolactivation&json=" + DCCarPoolActivationListJsonUtil.WriteFromObject(dcclass), cb);
        }

        internal void SetCarPoolActivation(DCCarPoolActivationList dcclass, CarPoolActivationListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(carpoolactivation_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=setcarpoolactivation&json=" + DCCarPoolActivationListJsonUtil.WriteFromObject(dcclass), cb);
        }

        void carpoolactivation_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            CarPoolActivationListCallback cb = (CarPoolActivationListCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCCarPoolActivationList dcclass = DCCarPoolActivationListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }


        internal void SaveRoute(DCRoute dcclass, RouteCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(route_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=saveroute&json=" + DCRouteJsonUtil.WriteFromObject(dcclass), cb);
        }

        internal void DeleteRoute(DCRoute dcclass, RouteCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(route_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=deleteroute&json=" + DCRouteJsonUtil.WriteFromObject(dcclass), cb);
        }

        void route_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            RouteCallback cb = (RouteCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCRoute dcclass = DCRouteJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

 
        internal void LoadRoutes(DCRouteList dcclass, RouteListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(routelist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=loadroutes&json=" + DCRouteListJsonUtil.WriteFromObject(dcclass), cb);
        }

        internal void GetRoutesInRect(DCRouteList dcclass, RouteListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(routelist_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=getroutesinrect&json=" + DCRouteListJsonUtil.WriteFromObject(dcclass), cb);
        }

        void routelist_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            RouteListCallback cb = (RouteListCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCRouteList dcclass = DCRouteListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        // --

        internal void SaveJoinRequest(DCJoinRequest dcclass, JoinRequestCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(joinrequest_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=savejoinrequest&json=" + DCJoinRequestJsonUtil.WriteFromObject(dcclass), cb);
        }

        void joinrequest_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            JoinRequestCallback cb = (JoinRequestCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCJoinRequest dcclass = DCJoinRequestJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }


        internal void GetMessages(DCMessageList dcclass, GetMessagesCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(getmessages_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=getmessages&json=" +DCMessageListJsonUtil.WriteFromObject(dcclass), cb);
        }

        void getmessages_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            GetMessagesCallback cb = (GetMessagesCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCMessageList dcclass = DCMessageListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        internal void DeleteCarpoolMembers(DCKarpoIdList dcclass, KarpoIdListCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(karpoids_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=deletecarpoolmembers&json=" + DCKarpoIdListJsonUtil.WriteFromObject(dcclass), cb);
        }

        void karpoids_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            KarpoIdListCallback cb = (KarpoIdListCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCKarpoIdList dcclass = DCKarpoIdListJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }

        // --

        internal void SendMessage(DCMessageOut dcclass, SendMessageCallback cb)
        {
            var webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            webClient.Encoding = Encoding.UTF8;

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(sendmessage_UploadStringCompletedEventHandler);
            webClient.UploadStringAsync(new Uri(url), "POST", "action=sendmessage&json=" + DCMessageOutJsonUtil.WriteFromObject(dcclass), cb);
        }

        void sendmessage_UploadStringCompletedEventHandler(object sender, UploadStringCompletedEventArgs e)
        {
            SendMessageCallback cb = (SendMessageCallback)e.UserState;
            if (e.Error != null)
                cb(false, e.Error.Message, null);
            else
            {
                DCMessageOut dcclass = DCMessageOutJsonUtil.ReadToObject(e.Result);
                MainPage.main.IndicateNewMessage(dcclass.newmessagesavailable);
                cb(true, "", dcclass);
            }
        }
    }
}
